//
//  ICMGetCarSearchTableViewCell.m
//  ICrEAM
//
//  Created by kuroro on 16/11/1.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "ICMGetCarSearchTableViewCell.h"

@interface ICMGetCarSearchTableViewCell()
@property (nonatomic,strong) UIView *carView;
@property (nonatomic,strong) UIView *vehicleTypeView;
@property (nonatomic,strong) UIView *stateView;
@property (nonatomic,strong) UIView *ownerView;
@property (nonatomic,strong) UIView *rightLine;
@property (nonatomic,strong) UIView *serverView;
@property (nonatomic,strong) UIView *preview;
@property (nonatomic,strong) UIView *whiteSepLine;
@end

@implementation ICMGetCarSearchTableViewCell


- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setUpICMGetCarSearchTableViewCellWithModel:(ICMGetCarSearchModel *)model{
    __weak typeof (self) weakSelf = self;
    self.contentView.backgroundColor = BackgroundColor_Gray;
    [self.carView removeFromSuperview];
    [self.vehicleTypeView removeFromSuperview];
    [self.stateView removeFromSuperview];
    [self.ownerView removeFromSuperview];
    [self.rightLine removeFromSuperview];
    [self.serverView removeFromSuperview];
    [self.preview removeFromSuperview];
    
    self.carView = [self setUpTimeViewWithLicenceNumber:model.licenceNumber time:model.time];
    //第一列
    [self.contentView addSubview:self.carView];
    [self.carView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.contentView.mas_left);
        make.top.equalTo(weakSelf.contentView.mas_top);
        make.width.equalTo(@(ActualWidth(188)));
        make.height.equalTo(@(94));
    }];
    
    
    //第二列
    self.vehicleTypeView = [self setUpLabelViewWithTitle:model.vehicleTypeName];
    [self addSubview:self.vehicleTypeView];
    [self.vehicleTypeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.carView.mas_right);
        make.top.equalTo(weakSelf.mas_top);
        make.height.equalTo(@(94));
        make.width.equalTo(@(ActualWidth(160)));
    }];
    
    //第三列
    self.stateView = [self setUpLabelViewWithTitle:model.state]; // 根据数据修改
    [self addSubview:self.stateView];
    [self.stateView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.vehicleTypeView.mas_right);
        make.top.equalTo(weakSelf.mas_top);
        make.height.equalTo(@(94));
        make.width.equalTo(@(ActualWidth(160)));
    }];
    
    //第四列
    self.ownerView = [self setUpLabelViewWithTitle:model.ownerName mobile:model.ownerMobile image:Appointment_Customer];
    [self addSubview:self.ownerView];
    [self.ownerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.stateView.mas_right);
        make.top.equalTo(weakSelf.mas_top);
        make.width.equalTo(@(ActualWidth(188)));
        make.height.equalTo(@(94));
    }];
    
    self.rightLine = [[UIView alloc]init];
    self.rightLine.backgroundColor = SepColor_gray;
    [self addSubview:self.rightLine];
    [self.rightLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.ownerView.mas_top);
        make.right.equalTo(self.ownerView.mas_right);
        make.height.equalTo(@(94));
        make.width.equalTo(@(1));
    }];
    //
    //第五列
    self.serverView = [self setUpLabelViewWithTitle:model.serverName mobile:model.serverMobile image:Appointment_Consultant];
    [self addSubview:self.serverView];
    [self.serverView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.mas_top);
        make.left.equalTo(self.ownerView.mas_right);
        make.width.equalTo(@(ActualWidth(188)));
        make.height.equalTo(@(94));
    }];
    //
    self.preview = [self setPreviewButton];
    [self addSubview:self.preview];
    [self.preview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(weakSelf.mas_centerY);
        make.right.equalTo(weakSelf.mas_right).offset(ActualWidth(-65));
        make.width.equalTo(@(30));
        make.height.equalTo(@(60));
    }];
    //
    [self.whiteSepLine removeFromSuperview];
    self.whiteSepLine = [[UIView alloc]initWithFrame:CGRectMake(0, 94, ScreenWidth, 1)];
    self.whiteSepLine.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:self.whiteSepLine];
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

- (UIView *)setUpTimeViewWithLicenceNumber:(NSString *)licenceNumber time:(NSString *)time{
    UIView *view = [[UIView alloc]init];
    view.backgroundColor = BackgroundColor_Gray;
    
    UILabel *licenceNumberLabel = [[UILabel alloc]init];
    licenceNumberLabel.font = NaviFont;
    licenceNumberLabel.textColor = ContentTextColor_Black;
    licenceNumberLabel.textAlignment = NSTextAlignmentCenter;
    licenceNumberLabel.text = licenceNumber;
    [view addSubview:licenceNumberLabel];
    [licenceNumberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(view.mas_centerX);
        make.centerY.equalTo(view.mas_centerY).offset(-16);
        make.width.equalTo(@(ActualWidth(188)));
        make.height.equalTo(@18);
    }];
    
    UILabel *timeLabel = [[UILabel alloc]init];
    timeLabel.font = ContentFont;
    timeLabel.textColor = ContentTextColor_Gray;
    timeLabel.textAlignment = NSTextAlignmentCenter;
    timeLabel.text = [ICrEAMTool getDateHourAndMinWithTimeInterval:time];
    [view addSubview:timeLabel];
    [timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(view.mas_centerX);
        make.centerY.equalTo(view.mas_centerY).offset(16);
        make.width.equalTo(@(ActualWidth(188)));
    }];
    
    UIView *leftLine = [[UIView alloc]init];
    leftLine.backgroundColor = SepColor_gray;
    [view addSubview:leftLine];
    [leftLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(view.mas_top);
        make.right.equalTo(view.mas_right);
        make.height.equalTo(@(94));
        make.width.equalTo(@(1));
    }];
    
    
    return view;
}

- (UIView *)setUpLabelViewWithTitle:(NSString *)title{
    UIView *view = [[UIView alloc]init];
    view.backgroundColor = BackgroundColor_Gray;
    
    UILabel *titleLabel = [[UILabel alloc]init];
    titleLabel.font = NaviFont;
    titleLabel.textColor = ContentTextColor_Black;
    titleLabel.text = title;
    titleLabel.textAlignment = NSTextAlignmentCenter;
    [view addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(view.mas_centerY);
        make.centerX.equalTo(view.mas_centerX);
        make.left.equalTo(view.mas_left);
        make.right.equalTo(view.mas_right);
    }];
    
    UIView *lineView = [[UIView alloc]init];
    lineView.backgroundColor = SepColor_gray;
    [view addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(view.mas_right);
        make.height.equalTo(@(94));
        make.width.equalTo(@(1));
    }];
    
    return view;
}

- (UIView *)setUpLabelViewWithTitle:(NSString *)title mobile:(NSString *)mobile image:(NSString *)image{
    UIView *view = [[UIView alloc]init];
    view.backgroundColor = BackgroundColor_Gray;
    
    UIImageView *imageView = [[UIImageView alloc]init];
    imageView.image = [UIImage imageNamed:image];
    [view addSubview:imageView];
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(view.mas_centerY);
        make.left.equalTo(view.mas_left).offset(ActualWidth(30));
        make.width.equalTo(@(45));
        make.height.equalTo(@(45));
    }];
    
//    UILabel *nameLabel = [[UILabel alloc]init];
//    nameLabel.font = NaviFont;
//    nameLabel.textColor = ContentTextColor_Black;
//    nameLabel.text = title;
//    nameLabel.numberOfLines = 2;
//    [view addSubview:nameLabel];
//    [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(imageView.mas_right).offset(3);
//        make.top.equalTo(view.mas_top).offset(20);
//        make.right.equalTo(view.mas_right).offset(-3);
//    }];
//    
//    UILabel *mobileLabel = [[UILabel alloc]init];
//    mobileLabel.font = ContentFont;
//    mobileLabel.textColor = ContentTextColor_Gray;
//    mobileLabel.text = mobile;
//    [view addSubview:mobileLabel];
//    [mobileLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(imageView.mas_right).offset(3);
//        make.bottom.equalTo(view.mas_bottom).offset(-20);
//    }];
    
    UIView *labelView = [[UIView alloc] init];
    UILabel *nameLabel = [[UILabel alloc]init];
    nameLabel.font = NaviFont;
    nameLabel.textColor = ContentTextColor_Black;
    nameLabel.text = title;
    nameLabel.numberOfLines = 2;
    [labelView addSubview:nameLabel];
    [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(labelView.mas_left);
        make.right.equalTo(labelView.mas_right);
    }];
    
    UILabel *mobileLabel = [[UILabel alloc]init];
    mobileLabel.font = ContentFont;
    mobileLabel.textColor = ContentTextColor_Gray;
    mobileLabel.text = mobile;
    [labelView addSubview:mobileLabel];
    [mobileLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(labelView.mas_left);
        make.right.equalTo(labelView.mas_right);
        make.top.equalTo(nameLabel.mas_bottom).offset(5);
//        make.right.equalTo(view.mas_right);
    }];
    
    
    [view addSubview:labelView];
    [labelView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(imageView.mas_right).offset(3);
        make.right.equalTo(view.mas_right).offset(-3);
        make.top.equalTo(nameLabel.mas_top);
        make.bottom.equalTo(mobileLabel.mas_bottom);
        make.centerY.equalTo(view.mas_centerY);
    }];
    
    return view;
}

-(UIView *)setPreviewButton{
    UIView *view = [[UIView alloc]init];
    
    UIImageView *imageView = [[UIImageView alloc]init];
    imageView.image = [UIImage imageNamed:Common_Preview];
    [view addSubview:imageView];
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(view.mas_top);
        make.centerX.equalTo(view.mas_centerX);
        make.height.equalTo(@(30));
        make.width.equalTo(@(30));
    }];
    
    UILabel *Label = [[UILabel alloc]init];
    Label.textColor = TitleColor;
    Label.font = ContentFont;
    Label.text = @"预览";
    [view addSubview:Label];
    [Label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(view.mas_bottom);
        make.centerX.equalTo(view.mas_centerX);
    }];
    
    UIButton *previewButton= [UIButton buttonWithType:UIButtonTypeCustom];
    
    [previewButton addTarget:self action:@selector(clickPreView) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:previewButton];
    [previewButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(view.mas_top);
        make.left.equalTo(view.mas_left);
        make.bottom.equalTo(view.mas_bottom);
        make.right.equalTo(view.mas_right);
    }];
    return view;
}

-(void)clickPreView{
    
    NSIndexPath *indexPatch = [((UITableView *)self.superview.superview) indexPathForCell:self];
    if ([self.delegate respondsToSelector:@selector(didClickPreViewWithIndex:)]) {
        [self.delegate didClickPreViewWithIndex:indexPatch];
    }
}


@end
